package com.deyuanyun.pic.settings.support;

import com.deyuanyun.pic.common.util.ConvertUtil;
import com.deyuanyun.pic.common.util.PassWordUtil;
import com.deyuanyun.pic.settings.controller.dto.UserSessionVO;
import com.deyuanyun.pic.settings.controller.dto.prvlg.UserVO;
import com.deyuanyun.pic.settings.domain.prvlg.CustomUser;
import com.deyuanyun.pic.settings.support.cache.SessionCacheSupport;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.session.Session;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Date;

/**
 * @ClassName: SecurityUtils
 *
 * @Description: 安全框架处理类
 * @author huanglw
 * @date 2015-8-1 上午10:58:22
 * @updater
 * @version V1.0.0
 *
 * @remarks:
 */
public class SecurityUtils {

    //public static User user = null;

    public static void setUser(CustomUser u){
        Session session = org.apache.shiro.SecurityUtils.getSubject().getSession();
        String sid = session.getId().toString();
        session.setAttribute(SessionCacheSupport.USERLOGINID, u.getUsername());

        UserSessionVO sessionVO=new UserSessionVO();
        ConvertUtil.coverWithSpring(u,sessionVO);
        sessionVO.setSessionID(sid);
        sessionVO.setLastLoginTime(new Date());
        sessionVO.setMoblie(u.getMoblie());
        //sessionVO.setPwd("");
        SessionCacheSupport.put(sessionVO);

        UserVO userVO = sessionVO.getUserDetailInfo();
        if(userVO != null) {
            session.setAttribute(SessionCacheSupport.USERREALNAME, userVO.getRealName());
        }

        //todo 此处可以增加登录日志
    }

    /**
     * 得到当前登录用户
     *
     * @return com.deyuanyun.pic.domain.settings.prvlg.User 返回当前登录用户
     */
    /*public static User currentUser(){
        return user == null ? new User() : user;
    }*/

    public static UserSessionVO currentUserSessionVO(){
        return SessionCacheSupport.getSessionVO();
    }

    public static void removeUser(HttpServletRequest request){
        UserSessionVO sessionVO= SessionCacheSupport.getSessionVO();
        if (sessionVO==null){
            return;
        }
        String id=sessionVO.getServletSessionID();
        if (StringUtils.isNotBlank(id)){
            SessionCacheSupport.removeRef(id);
        }
        SessionCacheSupport.remove(sessionVO.getUsername());
        HttpSession session = request.getSession();
        session.invalidate();
    }


//    public static User currentUser() {
//        try {
//            Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
//
//            return authentication == null ? null : (User) authentication.getPrincipal();
//        } catch (Exception e) {
//            throw new PmcBussnissException(ErrorCode.CUSTOM_ERROR, "没有找到登录用户");
//        }
//    }
//

    /**
     * 密码加密工具
     * @param userName
     * @param pwd
     * @return
     */
    public static String encodePassword(String userName, String pwd) {
        return PassWordUtil.encodePassword(userName,pwd);
    }
//
//    public static Member currentUserInPersist() {
//        Member user = currentUser();
//        return Member.read(Member.class, user.getId());
//    }
//
//    public static Member currentUserInPersistSession() {
//        try {
//            Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
//            return authentication == null ? null : (Member) authentication.getPrincipal();
//        } catch (Exception e) {
//            return null;
//        }
//    }
}
